//
//  AnimUtils.m
//  JiaoX
//
//  Created by yinqy on 15/1/22.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "AnimUtils.h"

@implementation AnimUtils

+(void)praiseAnim:(UIButton *)btn{
    UILabel *label = [[UILabel alloc]init];
    UIFont *font = [UIFont boldSystemFontOfSize:12];
    [label setFont:font];
    [label setTextColor:ThemeColor];
    
    NSString *str = @"+1";
    CGSize size = [str sizeWithFont:font];
    [label setFrame:CGRectMake(btn.x+(btn.width-size.width)/2, btn.y-size.height/2, size.width, size.height)];
    [label setText:str];
    [[btn superview] addSubview:label];
    
    CGFloat endY = label.y -label.height*2;
    [UIView animateWithDuration:0.5 animations:^{
        [label setY:endY];
        [label setAlpha:0];
    } completion:^(BOOL finished) {
        [label removeFromSuperview];
    }];
}

+(void)addToCartAnim:(UIView *)view startView:(UIView *)startView endView:(UIView *)endView delegate:(id)delegate adverse:(int)adverse{
    
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect startViewRect=[startView convertRect: startView.bounds toView:window];
    CGRect endViewRect = [endView convertRect: endView.bounds toView:window];
    
    CGPoint startPoint = CGPointMake(startViewRect.origin.x+startView.width/2, startViewRect.origin.y+startView.height/2);
    CGPoint endPoint = CGPointMake(endViewRect.origin.x+endView.width/2, endViewRect.origin.y+endView.height/2);
    
    //抛物线动画
    CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
    CGPathAddQuadCurveToPoint(thePath, NULL, startPoint.x-(view.width/3*2*adverse), startPoint.y+10*adverse, endPoint.x, endPoint.y);
    bounceAnimation.path = thePath;
    
    //缩放动画
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1];
    scaleAnimation.toValue = [NSNumber numberWithFloat:0.005];
    
    //动画集合
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray arrayWithObjects:bounceAnimation,scaleAnimation, nil];
    group.duration = .8;
    group.delegate = delegate;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    [view.layer addAnimation:group forKey:@"group"];
}

@end
